
package team2.game.crow;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.entity.primitive.Rectangle;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.Scene.IOnSceneTouchListener;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.text.ChangeableText;
import org.anddev.andengine.entity.text.Text;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.font.Font;
import org.anddev.andengine.opengl.font.FontFactory;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
import org.anddev.andengine.util.Debug;
import org.andengine.shmup.Boss;
import org.andengine.shmup.Enemy;
import org.andengine.shmup.EnemyPool;
import org.andengine.shmup.FinalBoss;
import org.andengine.shmup.GameContainer;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;

import android.content.Intent;
import android.content.res.Resources.NotFoundException;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

public class CrowActivity extends LoadingGameActivity implements IOnSceneTouchListener {
	
	// ===========================================================
	// Constants
	// ===========================================================
	
	public static final int CAMERA_WIDTH = 480;
	public static final int CAMERA_HEIGHT = 720;
	
	private static final int LAYER_COUNT = 4;
	
	public static final int LAYER_BACKGROUND = 0;//0
	public static final int LAYER_FOREGROUND = LAYER_BACKGROUND + 1;//1
	public static final int LAYER_BULLETS = LAYER_FOREGROUND +1;//2
	public static final int LAYER_UI = LAYER_BULLETS + 1;//3
	
	// ===========================================================
	// Fields
	// ===========================================================
	
	public Scene scene;
	public TiledTextureRegion laserUp;
	public TiledTextureRegion oneUp;
	public ChangeableText scoreText;
	public ChangeableText livesText;
	public int score;
	public HealthBar health;
	public ArrayList<Laser> enemyShots;
	public SpritePool lPool;
	public SpritePool enemyLaserPool;
	public List<Laser> shots;
	public List<Enemy> enemies;
	public ArrayList<PowerUp> powerUps;
	public EnemyPool bladerPool;
	public EnemyPool gruntPool;
	public Boss currentBoss;
	public FinalBoss finalBoss;
	public AnimatedSprite bossWeakPoint;
	public boolean levelDone;
	public AnimatedSpritePool hitSparks;
	public TiledTextureRegion explosion;
	public TiledTextureRegion mFirstBoss;
	public TiledTextureRegion mSecondBossCore;
	public TiledTextureRegion mThirdBossCore;
	public TiledTextureRegion mFourthBossCore;
	public TiledTextureRegion mFifthBoss;
	public TiledTextureRegion mFifthBossCore;
	public TiledTextureRegion mSixthBoss;
	public TiledTextureRegion mSixthBossCore;
	public TiledTextureRegion mSeventhBoss;
	public TiledTextureRegion mSeventhBossCore;
	public TiledTextureRegion mEigthBoss;
	public TiledTextureRegion mEigthBossCore;
	public TiledTextureRegion mNinthBoss;
	public TiledTextureRegion mTenthBoss;
	public TiledTextureRegion mTenthBoss2;
	public TextureRegion backgroundOne1;
	public TextureRegion backgroundOne2;
	public TextureRegion backgroundOne3;
	public TextureRegion backgroundOne4;
	public TextureRegion backgroundTwo1;
	public TextureRegion backgroundTwo2;
	public TextureRegion backgroundTwo3;
	public TextureRegion backgroundTwo4;
	public TextureRegion backgroundThree1;
	public TextureRegion backgroundThree2;
	public TextureRegion backgroundThree3;
	public TextureRegion backgroundThree4;
	public TextureRegion backgroundFour1;
	public TextureRegion backgroundFour2;
	public TextureRegion backgroundFour3;
	public TextureRegion backgroundFour4;
	public TextureRegion backgroundFive1;
	public TextureRegion backgroundFive2;
	public TextureRegion backgroundFive3;
	public TextureRegion backgroundFive4;
	public TextureRegion backgroundSix1;
	public TextureRegion backgroundSix2;
	public TextureRegion backgroundSix3;
	public TextureRegion backgroundSix4;
	public TextureRegion backgroundSeven1;
	public TextureRegion backgroundSeven2;
	public TextureRegion backgroundEight1;
	public TextureRegion backgroundEight2;
	public TextureRegion backgroundEight3;
	public TextureRegion backgroundEight4;
	public TextureRegion backgroundNine1;
	public TextureRegion backgroundNine2;
	public TextureRegion backgroundNine3;
	public TextureRegion backgroundNine4;
	public TextureRegion backgroundTen1;
	public TextureRegion backgroundTen2;
	public TextureRegion backgroundTen3;
	public TextureRegion backgroundTen4;
	public Rectangle blankScreen;
	public HealthBar bossHealth;
	public TiledTextureRegion explosionPart1;
	public TiledTextureRegion explosionPart2;
	public TiledTextureRegion shipExplosion;
	public TextureRegion bossHealthFrame;
	public Sprite bossHealthSprite;
	public TextureRegion goodLuck;
	public Rectangle bossBox;
	
	private Camera camera;
	private BitmapTextureAtlas spriteBox;
	private BitmapTextureAtlas spriteBox1;
	private BitmapTextureAtlas spriteBox2;	
	private BitmapTextureAtlas backgroundTexture1;
	private BitmapTextureAtlas backgroundTexture2;
	private BitmapTextureAtlas backgroundTexture3;
	private BitmapTextureAtlas levelSprites;
	private TiledTextureRegion munnin;
	private TiledTextureRegion cockpit;
	private TiledTextureRegion boosters;
	private TiledTextureRegion laserShooter;
	public TiledTextureRegion superLaser;
	public TiledTextureRegion bossSuperLaser;
	private TiledTextureRegion grunt;
	private TiledTextureRegion blader;
	public TiledTextureRegion levelNumbers;
	public TextureRegion levelSprite;
	public TextureRegion getReady;
	private TextureRegion healthFrame;
	private TextureRegion afterImage;
	private TextureRegion background;
	private TextureRegion gameOverBackground;
	private TextureRegion laser;
	private TextureRegion sButton;
	private TextureRegion eLaser;
	public Ship crow;
	private Sprite superButton;
	private Serializer s;
	private GameContainer gc;
	public int currentLevel;
	private Texture fontBox;
	public Font font;
	private ArrayList<Boss> bosses;
	public TiledTextureRegion mFourthBoss;
	public TiledTextureRegion mThirdBoss;
	public TiledTextureRegion mSecondBoss;
	private TextureRegion mBullet;
	private TiledTextureRegion mFinalLaser;
	private BitmapTextureAtlas bossSprites1;
	private BitmapTextureAtlas bossSprites2;
	
	// ===========================================================
		// Constructors
		// ===========================================================
	
	// ===========================================================
	// Getter & Setter
	// ===========================================================
	
	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================
	
	@Override
	public Engine onLoadEngine() {
		return super.onLoadEngine();
	}
	
	@Override
	public void onLoadResources() {
		super.onLoadResources();
	}
	
	@Override
	public Scene onLoadScene() {
		return super.onLoadScene();
	}
	
	@Override
	public void onLoadComplete() {
	}
	
	@Override
	protected void assetsToLoad() {
		//Sprites
		this.spriteBox = new BitmapTextureAtlas(1024,1024);
		this.superLaser = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.spriteBox, this, "FinalLaser.png",0,0, 3, 1);
		this.munnin = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.spriteBox, this, "Munnin.png",273,0,4,4);
		this.laser = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.spriteBox, this, "Laser.png",589,0);
		this.eLaser = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.spriteBox, this, "EnemyLaser.png",589,12);
		this.cockpit = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.spriteBox, this, "SuperCockpit.png",599,0,4,4);
		this.boosters = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(spriteBox, this, "CrowBooster.png",679,0, 4, 2);
		this.laserShooter = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.spriteBox, this, "LaserShooter.png",771,0, 5, 4);
		this.grunt = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.spriteBox, this, "Grunt.png",589,80, 3, 2);
		this.blader = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.spriteBox, this, "Blader.png",273,315, 6, 1);
		this.laserUp = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.spriteBox, this, "PowerUp.png", 691,62,2, 2);
		this.sButton = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.spriteBox, this, "Superbutton.png", 896, 57);
		this.oneUp = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.spriteBox, this, "Lifeup.png", 275,428,3, 2);
		this.afterImage = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.spriteBox, this, "Afterimage.png", 591, 189);
		this.explosion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.spriteBox, this, "Explosion.png", 673, 189,2 ,2);
		this.healthFrame = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.spriteBox, this, "Healthbar.png", 848, 57);
		this.levelNumbers = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.spriteBox, this, "LevelNumbers.png", 497, 440,5 ,2);
		this.levelSprite = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.spriteBox, this, "Level.png", 497, 560);
		this.getReady = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.spriteBox, this, "GetReady.png", 493, 645);
		this.shipExplosion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.spriteBox, this, "ShipExplosion.png", 0, 726,4 ,2);
		this.bossHealthFrame = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.spriteBox, this, "BossHealthbar.png", 889, 115);
		this.goodLuck = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.spriteBox, this, "GoodLuck.png", 487, 743);
		this.mEngine.getTextureManager().loadTexture(this.spriteBox);
		
		this.spriteBox1 = new BitmapTextureAtlas(1024,1024);
		this.explosionPart1 = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.spriteBox1, this, "BossExplosionOne.png", 0, 0, 2, 2);
		this.mEngine.getTextureManager().loadTexture(this.spriteBox1);
		
		this.spriteBox2 = new BitmapTextureAtlas(1024,1024);
		this.explosionPart2 = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.spriteBox2, this, "BossExplosionTwo.png", 0, 0, 2, 2);
		this.mEngine.getTextureManager().loadTexture(this.spriteBox2);
		
		/*
		//Boss Stuff
		this.bossSprites1 = new BitmapTextureAtlas(1024, 1024);
		this.mFirstBoss = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.bossSprites1, this, "FirstBoss.png", 0, 0, 1,1);
		this.mSecondBoss = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.bossSprites1, this, "BossTwo.png", 493, 0, 1,1);
		this.mSecondBossCore = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.bossSprites1, this, "SecondBossCore.png", 0, 193, 4,1);
		this.mThirdBoss = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.bossSprites1, this, "ThirdBoss.png", 0, 286, 1,1);
		this.mThirdBossCore = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.bossSprites1, this, "ThirdBossCore.png", 309, 194, 3,2);
		this.mFourthBoss = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.bossSprites1, this, "FourthBoss.png", 276, 500, 2,1);
		this.mFourthBossCore = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.bossSprites1, this, "FourthBossCore.png", 309, 307, 3,1);
		this.mFifthBoss = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.bossSprites1, this, "FifthBoss.png", 275, 775, 1,1);
		this.mFifthBossCore = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.bossSprites1, this, "FifthBossCore.png", 735, 751, 3,1);
		this.mEngine.getTextureManager().loadTexture(this.bossSprites1);
		
		this.bossSprites2 = new BitmapTextureAtlas(1024,1024);
		this.mSixthBoss = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.bossSprites2, this, "SixthBoss.png", 0, 0, 1,1);
		this.mSixthBossCore = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.bossSprites2, this, "SixthBossCore.png", 305, 0, 4,2);
		this.mSeventhBoss = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.bossSprites2, this, "SeventhBoss.png", 0, 307, 1,1);
		this.mSeventhBossCore = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.bossSprites2, this, "SeventhBossCore.png", 305, 162, 4,2);
		this.mEigthBoss = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.bossSprites2, this, "EigthBoss.png", 538, 307, 1,1);
		this.mEigthBossCore = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.bossSprites2, this, "EigthBossCore.png", 625, 0, 1,1);
		this.mNinthBoss = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.bossSprites2, this, "NinthBoss.png", 750, 0, 2,2);
		this.mTenthBoss = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.bossSprites2, this, "Huginn.png", 658, 564, 1,1);
		this.mTenthBoss2 = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.bossSprites2, this, "HuginnFinal.png", 625, 52, 1,1);
		this.mEngine.getTextureManager().loadTexture(this.bossSprites2);
		
		//Backgrounds
		this.backgroundTexture1 = new BitmapTextureAtlas(1024, 1024);
		this.backgroundOne1 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.backgroundTexture1, this, "FirstBackgroundOne.png", 0, 0);
		this.backgroundOne2 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.backgroundTexture1, this, "FirstBackgroundtwo.png", 240, 0);
		this.backgroundOne3 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.backgroundTexture1, this, "FirstBackgroundThree.png", 480, 0);
		this.backgroundOne4 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.backgroundTexture1, this, "FirstBackgroundFour.png", 720, 0);
		this.backgroundTwo1 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.backgroundTexture1, this, "SecondBackgroundOne.png", 0, 240);
		this.backgroundTwo2 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.backgroundTexture1, this, "SecondBackgroundTwo.png", 240, 240);
		this.backgroundTwo3 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.backgroundTexture1, this, "SecondBackgroundThree.png", 480, 240);
		this.backgroundTwo4 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.backgroundTexture1, this, "SecondBackgroundFour.png", 720, 240);
		this.backgroundThree1 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.backgroundTexture1, this, "ThirdBackgroundOne.png", 0, 480);
		this.backgroundThree2 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.backgroundTexture1, this, "ThirdBackgroundtwo.png", 240, 480);
		this.backgroundThree3 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.backgroundTexture1, this, "ThirdBackgroundThree.png", 480, 480);
		this.backgroundThree4 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.backgroundTexture1, this, "ThirdBackgroundFour.png", 720, 480);
		this.backgroundFour1 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.backgroundTexture1, this, "FourthBackgroundOne.png", 0, 720);
		this.backgroundFour2 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.backgroundTexture1, this, "FourthBackgroundTwo.png", 240, 720);
		this.backgroundFour3 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.backgroundTexture1, this, "FourthBackgroundThree.png", 480, 720);
		this.backgroundFour4 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.backgroundTexture1, this, "FourthBackgroundFour.png", 720, 720);
		this.mEngine.getTextureManager().loadTexture(this.backgroundTexture1);
		
		this.backgroundTexture2 = new BitmapTextureAtlas(1024,1024);
		this.backgroundFive1 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.backgroundTexture2, this, "FifthBackgroundOne.png", 0, 0);
		this.backgroundFive2 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.backgroundTexture2, this, "FifthBackgroundTwo.png", 240, 0);
		this.backgroundFive3 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.backgroundTexture2, this, "FifthBackgroundThree.png", 480, 0);
		this.backgroundFive4 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.backgroundTexture2, this, "FifthBackgroundFour.png", 720, 0);
		this.backgroundSix1 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.backgroundTexture2, this, "SixthBackgroundOne.png", 0, 240);
		this.backgroundSix2 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.backgroundTexture2, this, "SixthBackgroundTwo.png", 240, 240);
		this.backgroundSix3 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.backgroundTexture2, this, "SixthBackgroundThree.png", 480, 240);
		this.backgroundSix4 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.backgroundTexture2, this, "SixthBackgroundFour.png", 720, 240);
		this.backgroundSeven1 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.backgroundTexture2, this, "SeventhBackgroundOne.png", 0, 480);
		this.backgroundSeven2 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.backgroundTexture2, this, "SeventhBackgroundTwo.png", 240, 480);
		this.backgroundEight1 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.backgroundTexture2, this, "EigthBackgroundOne.png", 0, 720);
		this.backgroundEight2 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.backgroundTexture2, this, "EigthBackgroundTwo.png", 240, 720);
		this.backgroundEight3 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.backgroundTexture2, this, "EigthBackgroundThree.png", 480, 720);
		this.backgroundEight4 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.backgroundTexture2, this, "EigthBackgroundFour.png", 720, 720); 
		this.mEngine.getTextureManager().loadTexture(this.backgroundTexture2);
		
		this.backgroundTexture3 = new BitmapTextureAtlas(1024,1024);
		this.backgroundNine1 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.backgroundTexture3, this, "NinthBackgroundOne.png", 0, 0);
		this.backgroundNine2 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.backgroundTexture3, this, "NinthBackgroundTwo.png", 240, 0);
		this.backgroundNine3 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.backgroundTexture3, this, "NinthBackgroundThree.png", 480, 0);
		this.backgroundNine4 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.backgroundTexture3, this, "NinthBackgroundFourth.png", 720, 0);
		this.backgroundTen1 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.backgroundTexture3, this, "TenthBackgroundOne.png", 0, 240);
		this.backgroundTen2 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.backgroundTexture3, this, "TenthBackgroundTwo.png", 240, 240);
		this.backgroundTen3 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.backgroundTexture3, this, "TenthBackgroundThree.png", 480, 240);
		this.backgroundTen4 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.backgroundTexture3, this, "TenthBackgroundFour.png", 720, 240);
		this.mEngine.getTextureManager().loadTexture(this.backgroundTexture3);
		*/
		
		
		//Fonts
		this.fontBox = new BitmapTextureAtlas(512,512,TextureOptions.BILINEAR);
		this.font = FontFactory.createFromAsset(this.fontBox, this, "Tempest.ttf", 30, true, Color.WHITE);
		this.mEngine.getTextureManager().loadTexture(this.fontBox);
		this.mEngine.getFontManager().loadFont(this.font);
		
		//Other
		this.shots = Collections.synchronizedList(new ArrayList<Laser>());
		this.enemies = Collections.synchronizedList(new ArrayList<Enemy>());
		this.lPool = new SpritePool(laser);
		this.bladerPool = new EnemyPool(blader,480,720);
		this.gruntPool = new EnemyPool(grunt, 480,720);
		this.powerUps = new ArrayList<PowerUp>();
		this.s = new Persister();
		this.gc = new GameContainer();
		this.score = 0;
		this.enemyLaserPool = new SpritePool(eLaser);
		this.enemyShots = new ArrayList<Laser>();
		this.levelDone = false;
		this.bosses = new ArrayList<Boss>();
		this.hitSparks = new AnimatedSpritePool(explosion);
		this.blankScreen = new Rectangle(0,0,500,800);
		this.blankScreen.setColor(0, 0, 0);
		this.bossBox = new Rectangle(0,0,0,0);
		this.bossBox.setAlpha(0.30f);
		this.bossBox.setColor(1.0f, 0, 0);
		
		//Level Parsing
		try {
			this.gc = s.read(GameContainer.class, this.getResources().openRawResource(R.raw.complete));
			
		} catch (NotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	@Override
	protected Scene onAssetsLoaded() {
		this.scene = new Scene(LAYER_COUNT);
		this.scene.setOnSceneTouchListener(this);
		
		//Ship sprite
		this.crow = new Ship(240, 500, this.cockpit , this.boosters, this.munnin, this.laserShooter, this.superLaser, this.laser, afterImage,shots, lPool, this); //Remove redundant things
		this.scene.getChild(LAYER_BULLETS).attachChild(crow);
		this.crow.initialize();
		
		//Super Button Sprite
		this.superButton = new Sprite(400,650,sButton){
			@Override
			public boolean onAreaTouched(TouchEvent event,float x, float y){
				switch(event.getAction()){
				case TouchEvent.ACTION_DOWN:
					crow.goSuper();
					break;
				}
				return true;
			}
		};
		this.scene.getChild(LAYER_UI).attachChild(superButton);
		this.scene.registerTouchArea(superButton);
		
		//Score
		this.scoreText = new ChangeableText(20, 680, this.font, "Score: 0            ");
		this.scene.getChild(LAYER_UI).attachChild(scoreText);
		
		//Lives
		this.livesText = new ChangeableText(250, 680, this.font, "Lives: "+crow.getLives());
		this.scene.getChild(LAYER_UI).attachChild(livesText);
		
		//Health bar
		Sprite Frame = new Sprite(5,423, healthFrame);
		this.health = new HealthBar(10, 430, 25, 200, crow.getHealth());
		this.health.setRotation(180.0f);
		this.scene.getChild(LAYER_UI).attachChild(health);
		this.scene.getChild(LAYER_UI).attachChild(Frame);        
		this.crow.setHealthBar(health);
		
		//Boss health Frame
		this.bossHealthSprite = new Sprite(425,423, bossHealthFrame);
		this.scene.getChild(LAYER_UI).attachChild(bossHealthSprite);
		this.bossHealthSprite.setVisible(false);
		
		//Boss Collision Box
		this.scene.getChild(LAYER_FOREGROUND).attachChild(bossBox);
		this.bossBox.setVisible(false);
		
		//Handler of the levels
		this.scene.registerUpdateHandler(new LevelHandler(gc, this, currentLevel));
		
		//Collision Detector
		this.mEngine.registerUpdateHandler(new CollisionHandler(crow, this));
		
		return scene;
	}
	
	public void onCreate(Bundle stuff){
		super.onCreate(stuff);
		String level = this.getIntent().getDataString();
		level = level.replace('/','0');
		this.currentLevel = Integer.parseInt(level);
	}
	
	
	@Override
	public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
		//Debug.d("x= "+pSceneTouchEvent.getX()+"y= "+pSceneTouchEvent.getY());
		if(pSceneTouchEvent.isActionDown()) {
			if(crow.hasControl())
				this.crow.setTarget(pSceneTouchEvent.getX(), pSceneTouchEvent.getY()-100);
			return true;
		}
		else if(pSceneTouchEvent.isActionMove()){
			if(crow.hasControl())
				this.crow.setTarget(pSceneTouchEvent.getX(),pSceneTouchEvent.getY()-100);
			return true;
		}
		
		return false;
	}
	
	public boolean onCreateOptionsMenu(Menu menu)
	{
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		return true;
	}
	
	public boolean onOptionsItemSelected(MenuItem item) 
	{
		Intent i = null;
		switch (item.getItemId()) 
		{
		case R.id.mainmenu://Returns you to the main menu
			i = new Intent(CrowActivity.this, MainMenu.class);
            startActivity(i);
			break;
		case R.id.levelselect://Takes you to the level select scene
			i = new Intent(CrowActivity.this, MainMenu.class);
			startActivity(i);
			break;
		case R.id.credit://Displays the games credits
			credits();
			break;
		}
		return true;
	}
	
	public void gameOver(){
		loadGameOver();
		Scene gameOver = new Scene(LAYER_COUNT);
		
		Sprite background = new Sprite(0,0, gameOverBackground);
		gameOver.getChild(LAYER_UI).attachChild(background);
		
		SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yy HH:mm"); 
		ScoreDB highScores = new ScoreDB(this);
		highScores.addScore(sdf.format(new Date()), score);
		
		displayHighScores(gameOver, highScores);
		
		
		this.scene.setIgnoreUpdate(true);
		mEngine.setScene(gameOver);
		
	}
	
	private void credits(){
		Scene credits = new Scene();
		
		this.scene.setIgnoreUpdate(true);
		mEngine.setScene(credits);
	}
	
	// ===========================================================
	// Methods
	// ===========================================================
	
	private void displayHighScores(Scene scene, ScoreDB hs){
		String[] scores = hs.getScores();
		int yoffset = 0;
		
		for(String score: scores){
			scene.getChild(LAYER_UI).attachChild(new Text(100,200+yoffset, this.font, score));
			yoffset += 50;
			if(yoffset > CAMERA_HEIGHT)
				break;
		}
	}
	
	private void loadGameOver(){
		this.mEngine.getTextureManager().unloadTexture(levelSprites);
		this.levelSprites = new BitmapTextureAtlas(512,1024);
		
		this.gameOverBackground = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.levelSprites, this, "GameOverScreen.png",0,0);
		
		this.mEngine.getTextureManager().loadTexture(levelSprites);
	}
	
	public void loadLevel(int level){
		this.mEngine.getTextureManager().unloadTexture(levelSprites);
		this.levelSprites = new BitmapTextureAtlas(1024,1024);
		switch(level){
		case 0:
			this.mFirstBoss = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.levelSprites, this, "FirstBoss.png", 0, 0, 1,1);
			this.backgroundOne1 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.levelSprites, this, "FirstBackgroundOne.png", 0, 180);
			this.backgroundOne2 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.levelSprites, this, "FirstBackgroundtwo.png", 240, 180);
			this.backgroundOne3 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.levelSprites, this, "FirstBackgroundThree.png", 480, 180);
			this.backgroundOne4 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.levelSprites, this, "FirstBackgroundFour.png", 720, 180);
			break;
		case 1:
			this.mFirstBoss = null;
			this.backgroundOne1 = null;
			this.backgroundOne2 = null;
			this.backgroundOne3 = null;
			this.backgroundOne4 = null;
			this.mSecondBoss = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.levelSprites, this, "BossTwo.png", 0, 0, 1,1);
			this.mSecondBossCore = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.levelSprites, this, "SecondBossCore.png", 481, 0, 4,1);
			this.backgroundTwo1 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.levelSprites, this, "SecondBackgroundOne.png", 0, 481);
			this.backgroundTwo2 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.levelSprites, this, "SecondBackgroundTwo.png", 240, 481);
			this.backgroundTwo3 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.levelSprites, this, "SecondBackgroundThree.png", 480, 481);
			this.backgroundTwo4 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.levelSprites, this, "SecondBackgroundFour.png", 720, 481);
			break;
		case 2:
			this.mSecondBoss = null;
			this.mSecondBossCore = null;
			this.backgroundTwo1 = null;
			this.backgroundTwo2 = null;
			this.backgroundTwo3 = null;
			this.backgroundTwo4 = null;
			this.mThirdBoss = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.levelSprites, this, "ThirdBoss.png", 0, 0, 1,1);
			this.mThirdBossCore = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.levelSprites, this, "ThirdBossCore.png", 241, 0, 3,2);
			this.backgroundThree1 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.levelSprites, this, "ThirdBackgroundOne.png", 0, 600);
			this.backgroundThree2 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.levelSprites, this, "ThirdBackgroundtwo.png", 240, 600);
			this.backgroundThree3 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.levelSprites, this, "ThirdBackgroundThree.png", 480, 600);
			this.backgroundThree4 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.levelSprites, this, "ThirdBackgroundFour.png", 720, 600);
			break;
		case 3:
			this.mThirdBoss = null;
			this.mThirdBossCore = null;
			this.backgroundThree1 = null;
			this.backgroundThree2 = null;
			this.backgroundThree3 = null;
			this.backgroundThree4 = null;
			this.mFourthBoss = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.levelSprites, this, "FourthBoss.png", 0, 0, 2,1);
			this.mFourthBossCore = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.levelSprites, this, "FourthBossCore.png", 481, 0, 3,1);
			this.backgroundFour1 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.levelSprites, this, "FourthBackgroundOne.png", 0, 480);
			this.backgroundFour2 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.levelSprites, this, "FourthBackgroundTwo.png", 240, 480);
			this.backgroundFour3 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.levelSprites, this, "FourthBackgroundThree.png", 480, 480);
			this.backgroundFour4 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.levelSprites, this, "FourthBackgroundFour.png", 720, 480);
			break;
		case 4:
			this.mFourthBoss = null;
			this.mFourthBossCore = null;
			this.backgroundFour1 = null;
			this.backgroundFour2 = null;
			this.backgroundFour3 = null;
			this.backgroundFour4 = null;
			this.mFifthBoss = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.levelSprites, this, "FifthBoss.png", 0, 0, 1,1);
			this.mFifthBossCore = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.levelSprites, this, "FifthBossCore.png", 404, 0, 3,1);
			this.backgroundFive1 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.levelSprites, this, "FifthBackgroundOne.png", 0, 124);
			this.backgroundFive2 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.levelSprites, this, "FifthBackgroundTwo.png", 240, 124);
			this.backgroundFive3 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.levelSprites, this, "FifthBackgroundThree.png", 480, 124);
			this.backgroundFive4 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.levelSprites, this, "FifthBackgroundFour.png", 720, 124);
			break;
		case 5:
			this.mFifthBoss = null;
			this.mFifthBossCore = null;
			this.backgroundFive1 = null;
			this.backgroundFive2 = null;
			this.backgroundFive3 = null;
			this.backgroundFive4 = null;
			this.mSixthBoss = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.levelSprites, this, "SixthBoss.png", 0, 0, 1,1);
			this.mSixthBossCore = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.levelSprites, this, "SixthBossCore.png", 305, 0, 4,2);
			this.backgroundSix1 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.levelSprites, this, "SixthBackgroundOne.png", 0, 301);
			this.backgroundSix2 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.levelSprites, this, "SixthBackgroundTwo.png", 240, 301);
			this.backgroundSix3 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.levelSprites, this, "SixthBackgroundThree.png", 480, 301);
			this.backgroundSix4 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.levelSprites, this, "SixthBackgroundFour.png", 720, 301);
			break;
		case 6:
			this.mSixthBoss = null;
			this.mSixthBossCore = null;
			this.backgroundSix1 = null;
			this.backgroundSix2 = null;
			this.backgroundSix3 = null;
			this.backgroundSix4 = null;
			this.mSeventhBoss = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.levelSprites, this, "SeventhBoss.png", 0, 0, 1,1);
			this.mSeventhBossCore = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.levelSprites, this, "SeventhBossCore.png", 481, 0, 4,2);
			this.backgroundSeven1 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.levelSprites, this, "SeventhBackgroundOne.png", 0, 361);
			this.backgroundSeven2 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.levelSprites, this, "SeventhBackgroundTwo.png", 240, 361);
			break;
		case 7:
			this.mSeventhBoss = null;
			this.mSeventhBossCore = null;
			this.backgroundSeven1 = null;
			this.backgroundSeven2 = null;
			this.mEigthBoss = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.levelSprites, this, "EigthBoss.png", 0, 0, 1,1);
			this.mEigthBossCore = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.levelSprites, this, "EigthBossCore.png", 481, 0, 1,1);
			this.backgroundEight1 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.levelSprites, this, "EigthBackgroundOne.png", 0, 241);
			this.backgroundEight2 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.levelSprites, this, "EigthBackgroundTwo.png", 240, 241);
			this.backgroundEight3 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.levelSprites, this, "EigthBackgroundThree.png", 480, 241);
			this.backgroundEight4 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.levelSprites, this, "EigthBackgroundFour.png", 720, 241);
			break;
		case 8:
			this.mEigthBoss = null;
			this.mEigthBossCore = null;
			this.backgroundEight1 = null;
			this.backgroundEight1 = null;
			this.backgroundEight1 = null;
			this.backgroundEight1 = null;
			this.mNinthBoss = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.levelSprites, this, "NinthBoss.png", 0, 0, 2,2);
			this.backgroundNine1 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.levelSprites, this, "NinthBackgroundOne.png", 0, 241);
			this.backgroundNine2 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.levelSprites, this, "NinthBackgroundTwo.png", 240, 241);
			this.backgroundNine3 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.levelSprites, this, "NinthBackgroundThree.png", 480, 241);
			this.backgroundNine4 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.levelSprites, this, "NinthBackgroundFourth.png", 720, 241);
			break;
		case 9:
			this.mNinthBoss = null;
			this.backgroundNine1 = null;
			this.backgroundNine2 = null;
			this.backgroundNine3 = null;
			this.backgroundNine4 = null;
			this.mTenthBoss = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.levelSprites, this, "Huginn.png", 0, 0, 1,1);
			this.mTenthBoss2 = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.levelSprites, this, "HuginnFinal.png", 361, 0, 1,1);
			this.backgroundTen1 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.levelSprites, this, "TenthBackgroundOne.png", 0, 361);
			this.backgroundTen2 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.levelSprites, this, "TenthBackgroundTwo.png", 240, 361);
			this.backgroundTen3 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.levelSprites, this, "TenthBackgroundThree.png", 480, 361);
			this.backgroundTen4 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.levelSprites, this, "TenthBackgroundFour.png", 720, 361);
			break;
		case 10:
			this.mTenthBoss = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.levelSprites, this, "Huginn.png", 0, 0, 1,1);
			this.mTenthBoss2 = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.levelSprites, this, "HuginnFinal.png", 361, 0, 1,1);
			this.backgroundTen1 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.levelSprites, this, "TenthBackgroundOne.png", 0, 361);
			this.backgroundTen2 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.levelSprites, this, "TenthBackgroundTwo.png", 240, 361);
			this.backgroundTen3 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.levelSprites, this, "TenthBackgroundThree.png", 480, 361);
			this.backgroundTen4 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.levelSprites, this, "TenthBackgroundFour.png", 720, 361);
			break;
		default:
			//Do nothing
			break;
		}
		System.gc();
		this.mEngine.getTextureManager().loadTexture(levelSprites);
	}
	
	// ===========================================================
	// Inner and Anonymous Classes
	// ===========================================================
	
}